﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Client
{
    /// <summary>
    /// Sprite
    /// </summary>
    [DisallowMultipleComponent]
    public class UISprite : Image
    {
        [System.NonSerialized]
        bool hasAdded = false;

        [System.NonSerialized]
        bool hasUnload = false;

        [SerializeField]
        private bool _gray = false;
        public bool Gray
        {
            get
            {
                return _gray;
            }
            set
            {
                if (_gray == value)
                {
                    return;
                }

                if (sprite == null)
                {
                    if (value)
                    {
                        _gray = value;
                        color = Color.black;
                    }
                    else
                    {
                        _gray = value;
                        color = targetColor;
                    }
                }
                else
                {
                    if (string.CompareOrdinal(material.shader.name, "UI/Gray") != 0)
                    {
                        if (!value)
                        {
                            return;
                        }
                        material = new Material(material);
                        material.shader = Shader.Find("UI/Gray");
                    }
                    else
                    {
                        material = new Material(material); ;
                    }
                    material.SetFloat("_Gray", value ? 1 : 0);
                }
            }
        }
        [SerializeField]
        private Color targetColor = Color.white;
        public override Color color
        {
            get
            {
                return base.color;
            }
            set
            {
                if (value != Color.black)
                {
                    targetColor = value;
                }
                base.color = value;
            }
        }

        public new Sprite sprite
        {
            get
            {
                return base.sprite;
            }
            set
            {
                if (value == base.sprite)
                {
                    return;
                }

                if (hasAdded)
                {
                    if (value == null || value.texture != base.sprite.texture)
                    {
                        if (Global.AssetManager != null)
                        {
                            Global.AssetManager.SubRefAtlas(base.sprite.texture.name, this.gameObject);
                            hasAdded = false;
                        }
                    }
                }

                if (value != null && !hasAdded)
                {
                    if (Global.AssetManager != null)
                    {
                        Global.AssetManager.AddRefAtlas(value.texture.name, this.gameObject);
                        hasAdded = true;
                    }
                }

                base.sprite = value;
            }
        }

        protected override void Awake()
        {
            if (base.sprite != null)
            {
                if (Global.AssetManager != null)
                {
                    Global.AssetManager.AddRefAtlas(base.sprite.texture.name, this.gameObject);
                    hasAdded = true;
                }
            }
            base.Awake();
        }

        protected override void OnDestroy()
        {
            if (Application.isPlaying && hasAdded && !hasUnload)
            {
                hasUnload = true;
                if (base.sprite != null && base.sprite.texture != null)
                {
                    if (Global.AssetManager != null)
                    {
                        Global.AssetManager.SubRefAtlas(base.sprite.texture.name, this.gameObject);
                    }
                }
            }
            base.OnDestroy();
        }
    }
}
